package commandes;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import bdd.DBS;
import beans.Location;
import beans.Logement;

public class CommandeMajLocation implements Commande {
	  private String next;

	  public CommandeMajLocation(String next) {
	        this.next = next;
	  }

	public String execute(HttpServletRequest req) throws Exception {
		HttpSession session = req.getSession(true);
		if(session.getAttribute("droit").equals("administrateur")) {
			DBS db = DBS.getInstance();
			
			if(req.getParameter("supprLogt")!=null && req.getParameter("supprCli")!=null){
				db.getDB_Location().deleteLocation(Integer.parseInt(req.getParameter("supprLogt")), Integer.parseInt(req.getParameter("supprCli")));
			}
			
			if(req.getParameter("mod")!=null){
				req.setAttribute("mod",req.getParameter("mod"));
			}else{
				req.setAttribute("mod",0);
			}
			
			String idCli = req.getParameter("clients");
			String duree = req.getParameter("duree");
			String idlogt = req.getParameter("id");
			
			if(duree !=null){
				Location l=new Location(Integer.parseInt(idlogt),Integer.parseInt(idCli), Integer.parseInt(duree));
				db.getDB_Location().updateLocation(l);
			}else if(idCli == null || duree== null){
				req.setAttribute("erreur", true);
			}
			
			List<Location> locs = db.getDB_Location().getLocations();
			req.setAttribute("locations", locs);
			return next;
		} else {
			return "index.jsp";
		}
	}

}